package com.core.jdbc;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SqlHelper {

    private static String url = "jdbc:mysql:///whole?characterEncoding=utf8";
    private static String username = "root";
    private static String password = "";

    private static volatile DruidDataSource dataSource;

    static {
        if (dataSource == null) {
            init();
        }

    }

    public static void init() {
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
    }


    public static Object sqlOperate(String sql, Map<String, Object> par) throws SQLException {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Object id = null;
        try {
            // 获得连接:
            Connection conn = getConnect();
            conn.setAutoCommit(true);
            // 编写SQL：
            sql = SqlUtil.parseSql(sql, par);
            System.out.println("处理后的sql:" + sql);
            pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            // 执行sql:
            pstmt.executeUpdate();
            rs = pstmt.getGeneratedKeys();

            if (rs.next()) {
                id = rs.getObject(1);

            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return id;
    }

    public static List<Map<String, Object>> queryMapList(String sql, Map<String, Object> par) throws SQLException {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            // 获得连接:
            Connection conn = getConnect();
            conn.setAutoCommit(true);
            // 编写SQL：
            sql = SqlUtil.parseSql(sql, par);
            System.out.println("处理后的sql:" + sql);
            pstmt = conn.prepareStatement(sql);
            // 执行sql:
            rs = pstmt.executeQuery();
            list = SqlUtil.handleResutSet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return list;
    }


    public static Map<String, Object> queryMap(String sql, Map<String, Object> par) throws SQLException {
        List<Map<String, Object>> list = queryMapList(sql, par);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    /**
     * @return
     */
    public static Connection getConnect() throws SQLException {
        Connection conn = null;

        // 获得连接:
        conn = getDataSource().getConnection();
        return conn;

    }

    public static void setDataSource(DruidDataSource dataSource) {
        SqlHelper.dataSource = dataSource;
    }

    public static DruidDataSource getDataSource() {
        return dataSource;
    }
}
